package Frame.common;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import controller.Controller;
import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.MatteBorder;

public class RegisterFrame extends JFrame {
	private Controller controller;// 在登录界面这个界面new了Controller
	private JPanel contentPane;
	private JPanel panel1;
	private JTextField tf1;
	private JPasswordField tf2;
	private JLabel lb3;
	private JLabel lb4;
	private JCheckBox cb1;
	private static final long serialVersionUID = 1L;
	private int index;
	ImageIcon[] imgs = { new ImageIcon(getClass().getResource("/logo/曙光.jpg")),
			new ImageIcon(getClass().getResource("/logo/楼梯.jpg")),
			new ImageIcon(getClass().getResource("/logo/晚霞.jpg")), };
	

	public RegisterFrame(Controller controller) {
		this.controller = controller;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); // 获取屏幕大小
		contentPane = new MyJPanel();
		//contentPane = new JPanel();
		contentPane.setBackground(SystemColor.text);
		contentPane.setLayout(null); 
		contentPane.add(panel1());
		contentPane.setBounds(0, 0, 1920, 1080);
		contentPane.setFocusable(true);//让面板获取焦点 避免焦点在第一个文本框上
		setContentPane(contentPane);
		Timer timer = new Timer(3800, new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				contentPane.repaint();
			}
		});
		timer.start();
		this.setTitle("图书管理系统");
		this.setBounds(0, 0, screenSize.width, screenSize.height);
		this.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);//这个就是设置窗口最大化
		//this.setResizable(false);// 禁用最大化按钮
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//只关闭当前窗口
		this.setVisible(true);
	}

	public JPanel panel1() {
		panel1 = new JPanel();
		panel1.setBackground(Color.WHITE);
		panel1.setLayout(null);
		
		JLabel lb1 = new JLabel("欢迎注册为读者");
		lb1.setHorizontalAlignment(SwingConstants.LEFT);
		lb1.setFont(new Font("幼圆", Font.BOLD, 50));
		lb1.setBounds(0, 0, 660, 63);
		panel1.add(lb1);
		
		JLabel lb2 = new JLabel("一本好书, 一座灯塔。");
		lb2.setHorizontalAlignment(SwingConstants.LEFT);
		lb2.setFont(new Font("幼圆", Font.BOLD, 30));
		lb2.setBounds(0, 89, 660, 32);
		panel1.add(lb2);
		
		tf1 = new JTextField("学号/教职工号");
		tf1.setBounds(0, 214, 660, 70);
		tf1.setFont(new Font("幼圆", Font.BOLD, 28));
		tf1.setForeground(Color.GRAY);
		panel1.add(tf1);
		tf1.setColumns(10);
		tf1.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e) {//获取焦点
				if(tf1.getText().equals("学号/教职工号")) {
					tf1.setText("");
					tf1.setForeground(Color.BLACK);
				}
			}

			@Override
			public void focusLost(FocusEvent e) {//失去焦点
				if(tf1.getText().equals("")) {
					tf1.setText("学号/教职工号");
					tf1.setForeground(Color.GRAY);
				}			
			}
		});
		panel1.setBounds(964, 116, 660, 703);
		
		tf2 = new JPasswordField("密码");
		tf2.setEchoChar((char) 0);//显示原本的内容	
		tf2.addFocusListener(new FocusListener(){
			@Override
			public void focusGained(FocusEvent e) {//获取焦点
				if(new String(tf2.getPassword()).equals("密码")) {
					tf2.setText("");
					tf2.setEchoChar('·');
					tf2.setForeground(Color.BLACK);
				}
			}

			@Override
			public void focusLost(FocusEvent e) {//失去焦点
				if(new String(tf2.getPassword()).equals("")) {
					tf2.setText("密码");
					tf2.setEchoChar((char) 0);//显示原本的内容 
					tf2.setForeground(Color.GRAY);
				}	
			}
		});
		tf2.setForeground(Color.GRAY);
		tf2.setColumns(10);
		tf2.setBounds(0, 352, 660, 70);
		tf2.setFont(new Font("幼圆", Font.BOLD, 28));
		panel1.add(tf2);
		
		JButton registerButton = new JButton("立即注册");
		registerButton.setForeground(SystemColor.text);
		registerButton.setBackground(SystemColor.textHighlight);
		registerButton.setFont(new Font("宋体", Font.PLAIN, 30));
		registerButton.setBounds(0, 489, 660, 63);
		registerButton.addActionListener(controller);
		registerButton.setActionCommand("register");
		panel1.add(registerButton);
		
		cb1 = new JCheckBox("我已阅读并同意相关服务条款和隐私政策");
		cb1.setFont(new Font("宋体", Font.PLAIN, 18));
		cb1.setBackground(SystemColor.text);
		cb1.setBounds(0, 632, 358, 27);
		panel1.add(cb1);
		
	    lb3 = new JLabel("请输入学号/教职工号");
		lb3.setForeground(new Color(255, 0, 0));
		lb3.setFont(new Font("幼圆", Font.PLAIN, 18));
		lb3.setBounds(0, 286, 185, 32);
		lb3.setVisible(false);//默认设为不可见
		panel1.add(lb3);
		
		lb4 = new JLabel("请输入密码");
		lb4.setForeground(Color.RED);
		lb4.setFont(new Font("幼圆", Font.PLAIN, 18));
		lb4.setBounds(0, 423, 185, 32);
		lb4.setVisible(false);//默认设为不可见
		panel1.add(lb4);
		return panel1;
	}

	class MyJPanel extends JPanel {
		@Override
		public void paint(Graphics g) {
			super.paint(g);
			g.drawImage(imgs[index % imgs.length].getImage(), 0, 0, contentPane);
			index++;
		}
	}

	public JTextField getTf1() {
		return tf1;
	}

	public void setTf1(JTextField tf1) {
		this.tf1 = tf1;
	}
	public JPasswordField getTf2() {
		return tf2;
	}

	public void setTf2(JPasswordField tf2) {
		this.tf2 = tf2;
	}

	public JLabel getLb3() {
		return lb3;
	}

	public void setLb3(JLabel lb3) {
		this.lb3 = lb3;
	}

	public JLabel getLb4() {
		return lb4;
	}

	public void setLb4(JLabel lb4) {
		this.lb4 = lb4;
	}

	public JCheckBox getCb1() {
		return cb1;
	}

	public void setCb1(JCheckBox cb1) {
		this.cb1 = cb1;
	}
	
}
